package com.common.interceptor;

import com.common.controller.Constant;
import com.exp.model.Admin;
import com.exp.model.Business;
import com.exp.model.User;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 Created by P0063305 on 2016/3/30.
 */
/*拦截器*/
public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拦截开始
        String viewPath = request.getServletPath();
        String[] exclude = {"/", "/WEB-INF/views/login/login.html", "/login/userLogin", "/login/businessLogin", "/login/adminLogin","/businessRegister","/register",
                "/WEB-INF/views/register/businessRegister.html","/WEB-INF/views/register/register.html",
               "/business/businessRegister","/business/register"};
        boolean flag = false;
        if (exclude != null && !"".equals(exclude)) {
            for (String param : exclude) {
                if (viewPath.equals(param)) {
                    flag = true;
                }
            }
        }
        if (flag) {
            return true;
        } else {
            User user = (User) request.getSession().getAttribute(Constant.SESSION_CURRENT_USER);
            Business business = (Business) request.getSession().getAttribute(Constant.SESSION_CURRENT_BUSINESS);
            Admin admin=(Admin)request.getSession().getAttribute(Constant.SESSION_CURRENT_ADMIN);

            if (user == null && business == null && admin==null) {
                response.sendRedirect(request.getContextPath());
            } else {
                return true;
            }
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

}
